import request from "./request";
import global from "./global.data.params";
import { useStoreData } from "@/stores/data";
const storeData: any = useStoreData();

import storages from "@/utils/storage";
/**
 * 系统请求函数，所有请求统一调用此文件函数
 */
const req = {
    /**
     * 获取图层树数据
     * @param baseId 基地ID
     */
    coverage: () => {
        if (storages.get("baseId")) {
            request
                .get("api/map/baseFolder/getTreeFolderByBaseId", { baseId: storages.get("baseId") })
                .then((res: any) => {
                    if (res.code == 200) {
                        storeData.treeData = res.data;
                        // req.mapplotting();
                    } else {
                        throw Error(res.message);
                    }
                })
                .catch((e) => {
                    throw Error(e);
                });
        } else {
            throw Error("coverage---baseId不能为空");
        }
    },
    /**
     * 获取地图图层数据
     * @param baseId 基地ID
     */
    mapplotting: () => {
        if (storages.get("baseId")) {
            request
                .get("/api/map/draw/getDrawingByBaseId", { baseId: storages.get("baseId") })
                .then((res: any) => {
                    if (res.code == 200) {
                        console.log(storeData.tree);
                        res.data.forEach((v: any) => {
                            let addNode = {
                                name: v.name,
                                pid: v.pid,
                                id: v.id,
                                type: v.type,
                                editor: true,
                                show: true,
                            };
                            let pNode = storeData.tree.getNode(v.pid);
                            if (pNode) {
                                storeData.tree.append(addNode, pNode);
                                // 设置新添加子菜单为选中状态
                                if (v.isShow == "1") {
                                    storeData.tree.setChecked(addNode, true, false);
                                }
                                // 渲染数据
                                // render(v)
                            }
                        });
                    } else {
                        throw Error(res.message);
                    }
                })
                .catch((e) => {
                    throw Error(e);
                });
        } else {
            throw Error("mapplotting---baseId不能为空");
        }
    },
};
export default req;
